﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

namespace LabMS.Web.Parameters
{
    public partial class Edit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                tbETime.Attributes.Add("readOnly", "true");
                tbSTime.Attributes.Add("readOnly", "true");
                tbETime.Attributes.Add("OnClick", "c.showMoreDay=false;c.show(this);");
                tbSTime.Attributes.Add("OnClick", "c.showMoreDay=false;c.show(this);");

                int ID =int.Parse(Request.QueryString["ID"].ToString());
                InitBind(ID);
            }
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            #region 验证
            if (string.IsNullOrEmpty(ddlYear.SelectedValue))
            {
                Common.JShelper.JSAlert(this.Page,"","学年不能为空！");
                ddlYear.Focus();
            }
            else if (string.IsNullOrEmpty(ddlAcadmic.SelectedValue))
            {
                Common.JShelper.JSAlert(this.Page,"","学期不能为空！");
                ddlAcadmic.Focus();
            }
            else if (string.IsNullOrEmpty(tbSTime.Text.Trim()))
            {
                Common.JShelper.JSAlert(this.Page,"","开学时间不能为空！");
                tbSTime.Focus();
            }
            else if (string.IsNullOrEmpty(tbETime.Text.Trim()))
            {
                Common.JShelper.JSAlert(this.Page,"","结束时间不能为空！");
                tbETime.Focus();
            }
            #endregion
            else
            {
                DateTime Stime =DateTime.Parse(tbSTime.Text.Trim());
                DateTime Etime =DateTime.Parse(tbETime.Text.Trim());
                int a=Etime.CompareTo(Stime);
                if (a != 1)
                {
                    Common.JShelper.JSAlert(this.Page, "", "结束时间不能早于开学时间！");
                    tbETime.Text = "";
                    tbETime.Focus();
                }
                else
                {
                    LabMS.Model.Semester MS = new LabMS.Model.Semester();
                    MS.ID = int.Parse(Request.QueryString["ID"].ToString());
                    MS.ETime = DateTime.Parse(tbETime.Text.Trim());
                    MS.STime = DateTime.Parse(tbSTime.Text.Trim());
                    MS.Year = int.Parse(ddlYear.SelectedValue.ToString());
                    MS.MSemester = int.Parse(ddlAcadmic.SelectedValue.ToString());
                    LabMS.BLL.Semester BS = new LabMS.BLL.Semester();
                    try
                    {
                        BS.Update(MS);
                        Common.JShelper.JSAlertAndRedirect(this.Page, "", "修改成功！", "academic-year.aspx");
                    }
                    catch
                    {
                        Common.JShelper.JSAlert(this.Page, "", "修改失败！");
                    }
                }
            }
        }

        /// <summary>
        /// 初始化页面赋值
        /// </summary>
        protected void InitBind(int ID)
        {
            string str = "ID="+ID;
            LabMS.BLL.Semester BS = new LabMS.BLL.Semester();
            List<LabMS.Model.Semester> list = new List<LabMS.Model.Semester>();
            list=BS.GetModelList(str);
            ddlYear.SelectedValue = list[0].Year.Value.ToString();
            ddlAcadmic.SelectedValue = list[0].MSemester.Value.ToString();
            tbETime.Text = list[0].ETime.Value.ToShortDateString();
            tbSTime.Text = list[0].STime.Value.ToShortDateString();

            LabMS.BLL.Dictionary BDicitonary = new LabMS.BLL.Dictionary();
            ExtendBLL.Dictionary EDictionary = new ExtendBLL.Dictionary();
            ddlYear.DataSource = EDictionary.GetItemListByDClass("ruxuenianfen");
            ddlYear.DataValueField = "Code";
            ddlYear.DataTextField = "Name";
            ddlYear.DataBind();
            ddlYear.SelectedValue = DateTime.Now.Year.ToString();
        }
    }
}
